export {}
console.log('1.类型的注解(Annotation)和推论(Inference) =======================')
// 注解: 变量手动定义变量类型
let v111: number = 18
// 推论: TS自动推断变量类型
let v112 = 28

// null undefined symbol
let v12null: null = null
let v12undefined: undefined = undefined
let v12symbol: symbol = Symbol('symbol')

console.log()
console.log()
console.log('2.Any,unknown类型及重要提示 =====================================')
// any没有类型检查
let v21any: any = 18
v21any = '18'
v21any = false

// 没有指定类型, 推断为any
let v22any
v22any = 'hi'

let v23: number = 18
// Error: 不能将类型“string”分配给类型“number”
// v23 = v22any

// unknown类型有类型检查(相当于安全的any)
let v24unknown: unknown = 18
let v25: boolean = false
// Error: 不能将类型“unknown”分配给类型“boolean”
// v25 = v24unknown
// 解决: 需要用处as关键字
v25 = v24unknown as boolean
if (typeof v24unknown === 'boolean') {
  v25 = v24unknown
}

console.log()
console.log()
console.log('3.As关键字 =====================================')

console.log()
console.log()
console.log('4.Void和never类型及重要提示 =====================================')
function v41fun(): void {
  console.log('void')
  return undefined
  //   return
}
// return null, 返回值只能是null
function v42fun(): null {
  return null
}

// never: 报错后, 之后代码会变灰, 无法到达
function v43Error(msg: string): never {
  throw new Error(msg)
}
// 同样返回值是never
let v44Error = () => {
  throw new Error('something go wrong')
}

// const v45Error = 114 // 此时v451Error推断类型为 never
const v45Error: number = 114 // 此时v451Error推断类型为 number
// let v45Error = 114 // 此时v451Error推断类型为 number
if (v45Error !== 114) {
  let v451Error = v45Error
}
// v43Error('something go wrong')
console.log('test error')

console.log()
console.log()
console.log('5.在TS中array类型的写法及method提示,Tuple类型 =================')
const v51booleanArr: boolean[] = [true, false, true]
const v52numberArr: string[] = []

let v53Tuple: [number, string, boolean] = [18, 'ace', true]
// 是可以调通的
v53Tuple.push('18')
// 解构赋值
let [v53age, v53name, v53flag] = v53Tuple
// let [v53age, v53name, v53flag, other] = v53Tuple // 多一个变量会报错, Error: 元组长度为3

console.log()
console.log()
console.log('6.字面量类型(Literal Type)====================================')
let v61: 'hello'
v61 = 'hello'
// v61 = 'hi' // Error: 不能将类型“"hi"”分配给类型“"hello"”

let v62: 'M' | 'F' | 'X'
// v61 = '' // Error: 不能将类型“""”分配给类型“"M" | "F" | "X"”

// 类型推断为hellohere
const v63 = 'hellohere'
console.log(typeof v63) // string;
